package com.hoshiicloud.lessee.controller.category;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.auth.vo.TokenUsersVO;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.common.utils.token.UserInfoUtil;
import com.hoshiicloud.common.utils.tree.GoodsCategoryWithGoodNumTree;
import com.hoshiicloud.common.utils.tree.Menu;
import com.hoshiicloud.goods.entity.goods.GoodsCategory;
import com.hoshiicloud.goods.entity.goods.GoodsSpecifications;
import com.hoshiicloud.goods.param.goods.GoodsCategoryStatusUpdateParam;
import com.hoshiicloud.goods.param.goods.GoodsPropertiesQueryParam;
import com.hoshiicloud.goods.param.goods.GoodsSpecificationsQueryParam;
import com.hoshiicloud.goods.vo.GoodsCategoryEditInfoVo;
import com.hoshiicloud.goods.vo.GoodsPropertiesPageVo;
import com.hoshiicloud.lessee.feign.LesseeGoodsFeignService;
import com.hoshiicloud.lessee.feign.LesseeUserFeignService;
import com.hoshiicloud.lessee.param.AddGoodCategoryJHParam;
import com.hoshiicloud.lessee.param.UpdateGoodCategoryJHParam;
import com.hoshiicloud.lessee.service.LesseeGoodCategoryService;
import com.hoshiicloud.user.entity.company.AttachmentType;
import com.hoshiicloud.user.param.company.AttachmentTypePageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 2019/8/9 11:54 qry
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/category/goods-category")
@Api(value = "品类属性-类目管理", tags = {"品类属性-类目管理"})
public class LesseeGoodsCategoryController {
    private final LesseeGoodsFeignService goodsFeignService;
    private final LesseeGoodCategoryService lesseeGoodCategoryService;
    private final LesseeUserFeignService lesseeUserFeignService;


    public LesseeGoodsCategoryController(LesseeGoodsFeignService goodsFeignService, LesseeGoodCategoryService lesseeGoodCategoryService, LesseeUserFeignService lesseeUserFeignService) {
        this.goodsFeignService = goodsFeignService;
        this.lesseeGoodCategoryService = lesseeGoodCategoryService;
        this.lesseeUserFeignService = lesseeUserFeignService;
    }

    /*@ApiOperation(value = "分页查询商品类目表")
    @PostMapping("/getPage")
    public Response<Page<GoodsCategory>> page(@RequestBody @Valid GoodsCategoryQueryParam param, HttpServletRequest rq) {
        param.setCompanyId(UserInfoUtil.getUserInfo(rq).getComanyId());
        return goodsFeignService.getGoodsCategoryPage(param);
    }*/


    @PostMapping("/addGoodsCategory")
    @ApiOperation(value = "新增商品类目表")
    public Response<Boolean> save(@RequestBody @Valid AddGoodCategoryJHParam param, HttpServletRequest rq) {
        TokenUsersVO user = UserInfoUtil.getUserInfo(rq);
        GoodsCategory goodsCategory;
        try {
            goodsCategory = BeanUtils.covertType(param,GoodsCategory.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        goodsCategory.setCompanyId(user.getCompanyId());
        goodsCategory.setCreatedBy(user.getRealName());
        return  Response.data(lesseeGoodCategoryService.addGoodsCategory(goodsCategory,param.getProIds(),param.getSpecIds()));
    }


    @PostMapping("/updateGoodsCategory")
    @ApiOperation("商品类目编辑")
    public Response<Boolean> updateGoodsCategory(@RequestBody @Valid UpdateGoodCategoryJHParam param, HttpServletRequest rq){
        TokenUsersVO user = UserInfoUtil.getUserInfo(rq);
        GoodsCategory goodsCategory;
        try {
            goodsCategory = BeanUtils.covertType(param,GoodsCategory.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        goodsCategory.setCompanyId(user.getCompanyId());
        goodsCategory.setUpdatedBy(user.getRealName());//目前写死 登录做完后要从token 拿
        return Response.data(lesseeGoodCategoryService.updateGoodsCategory(goodsCategory,param.getProIds(),param.getSpecIds()));
    }

    @GetMapping("/queryGoodsCategoryEditInfo/{id}")
    @ApiOperation("获取类目编辑页页面信息")
    public Response<GoodsCategoryEditInfoVo> queryGoodsCategoryEditInfo(@PathVariable("id") Long id){
        return Response.data(lesseeGoodCategoryService.queryGoodsCategoryEditInfo(id));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除商品类目表")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response<Boolean> delete(@PathVariable("id") Long id){
        return goodsFeignService.deleteGoodsCategory(id);
    }

    @ApiOperation(value = "商品类目查询,树状数据(不包含禁用)")
    @GetMapping("/getCategorys")
    public Response<List<Menu>> getCategorys(HttpServletRequest rq) {
        return goodsFeignService.getCategorys(UserInfoUtil.getUserInfo(rq).getCompanyId());
    }

    @ApiOperation(value = "商品类目查询带商品数量,树状数据")
    @GetMapping("/getCategorys2")
    public Response<List<GoodsCategoryWithGoodNumTree>> getCategorys2(HttpServletRequest rq) {
        return goodsFeignService.getCategorys2(UserInfoUtil.getUserInfo(rq).getCompanyId());
    }

    @ApiOperation(value = "商品类目查询,树状数据(含禁用)")
    @GetMapping("/getCategorys3")
    public Response<List<Menu>> getCategorys3(HttpServletRequest rq) {
        return goodsFeignService.getCategorys3(UserInfoUtil.getUserInfo(rq).getCompanyId());
    }

    @ApiOperation(value = "修改类目的启用禁用状态")
    @PutMapping("/updateStatus")
    public Response<Boolean> updateStatus(@RequestBody @Valid GoodsCategoryStatusUpdateParam param,HttpServletRequest rq) {
        param.setUpdatedBy(UserInfoUtil.getUserInfo(rq).getRealName());
        return goodsFeignService.updateGoodsCategoryStatus(param);
    }


    @ApiOperation("新增类目-选择参数属性分页")
    @PostMapping("/getPropertiesPage")
    public Response<Page<GoodsPropertiesPageVo>> getPropertiesPage(@RequestBody @Valid GoodsPropertiesQueryParam param, HttpServletRequest rq) {
        param.setCompanyId(UserInfoUtil.getUserInfo(rq).getCompanyId());
        Response<Page<GoodsPropertiesPageVo>> rs = goodsFeignService.getGoodsPropertiesPage(param);
        if(null == rs || rs.getCode() !=200 || null == rs.getData() ){
            return  Response.data(new Page<>(Long.valueOf(param.getPageNumber()),Long.valueOf(param.getPageSize())));
        }
        return rs;
    }

    @ApiOperation("新增类目-选择规格分页")
    @PostMapping("/getSpecPage")
    public Response<Page<GoodsSpecifications>> getSpecPage(@RequestBody @Valid GoodsSpecificationsQueryParam param , HttpServletRequest rq) {
        param.setCompanyId(UserInfoUtil.getUserInfo(rq).getCompanyId());
        Response<Page<GoodsSpecifications>> rs =  goodsFeignService.getGoodsSpecPage(param);
        if(null == rs || rs.getCode() !=200 || null == rs.getData() ){
            return  Response.data(new Page<>(Long.valueOf(param.getPageNumber()),Long.valueOf(param.getPageSize())));
        }
        return rs;
    }

    @ApiOperation("新增类目-选择证件分页")
    @PostMapping("/getPage")
    public Response<Page<AttachmentType>> getPage(@RequestBody AttachmentTypePageParam param,HttpServletRequest rq){
        param.setCompanyId(UserInfoUtil.getUserInfo(rq).getCompanyId());
        Response<Page<AttachmentType>> rs = lesseeUserFeignService.getAttachmentTypePage(param);
        if(null == rs || rs.getCode() !=200 || null == rs.getData() ){
            return  Response.data(new Page<>(Long.valueOf(param.getPageNumber()),Long.valueOf(param.getPageSize())));
        }
        return rs;
    }


}
